package com.tsystems.skilltest.service;

import com.tsystems.skilltest.PropertiesUtils;
import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * @author aesipov
 */
public class GetTransactionServicePublisher
{
    private static Properties properties   = PropertiesUtils.getProperties("skilltest.properties");

    private static final Logger logger = LoggerFactory.getLogger(GetTransactionServicePublisher.class);

    private GetTransactionService getTransactionService;

    public GetTransactionServicePublisher(GetTransactionService getTransactionService) {
        this.getTransactionService = getTransactionService;
    }

    public void publishService()
    {
        String address = properties.getProperty("soap.endpoint.url");

        ServerFactoryBean svrFactory = new ServerFactoryBean();
        svrFactory.setServiceClass(GetTransactionService.class);
        svrFactory.setAddress(address);
        svrFactory.setServiceBean(getTransactionService);
        svrFactory.getInInterceptors().add(new LoggingInInterceptor());
        svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
        svrFactory.create();

        logger.info("GetTransactionService was published on {}", address);
    }
}
